package com.caiyouxi.partner.service.impl;

import com.caiyouxi.common.bean.QueryParams;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.exception.ErrorCode;
import com.caiyouxi.common.service.BaseService;
import com.caiyouxi.partner.mapper.ChannelAppConfigMapper;
import com.caiyouxi.partner.model.ChannelAppConfigBean;
import com.caiyouxi.partner.service.ChannelAppConfigService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/11/21.
 */
@Service
public class ChannelAppConfigServiceImpl extends BaseService implements ChannelAppConfigService{

    private Logger logger = Logger.getLogger(ChannelAppConfigServiceImpl.class);

     @Autowired
     private ChannelAppConfigMapper channelAppConfigMapper;


    /**
     *获取游戏配置信息
     **/
    @Override
    public ResData get_app_config_info(ChannelAppConfigBean bean) {
        return  success(get_channel_app_config(bean)) ;
    }

    public ChannelAppConfigBean get_channel_app_config(ChannelAppConfigBean bean)
    {
        String key = "CHANNEL_APP_CONFIG"+bean.getApp_id()+bean.getChannel_id();
        ChannelAppConfigBean channelAppConfigBean = null;
        if (redisService.exists(key)){
            channelAppConfigBean =  (ChannelAppConfigBean)redisService.get(key);
        }
        /**缓存没有就查数据库*/
        if (StringUtils.isEmpty(channelAppConfigBean)){
            logger.info("查数据库");
            channelAppConfigBean = channelAppConfigMapper.select_channel_config(bean);
            /**数据添加到缓存*/
            redisService.set(key,channelAppConfigBean);
        }
        return channelAppConfigBean;
    }



    /**
     *获取游戏配置信息列表
     **/
    @Override
    public ResData get_app_config_list(ChannelAppConfigBean bean) {
        Integer reids_page_size = Integer.parseInt( redisService.get("sys.pagesize").toString());
        if(null != reids_page_size){
            bean.setPage_size(reids_page_size);
        }
        List<ChannelAppConfigBean> channelAppConfigList= channelAppConfigMapper.get_channel_config_list(bean);
        int total = channelAppConfigMapper.get_count(bean);
        QueryParams pageList = new QueryParams();
        pageList.setTotal(total);
        pageList.setPage(bean.getPage());
        pageList.setPage_size(bean.getPage_size());
        pageList.setList(channelAppConfigList);
        return success(pageList);
    }


    /**
     *新增游戏配置信息
     **/
    @Override
    @Transactional
    public ResData add_app_config(ChannelAppConfigBean bean) {
        bean.setCreate_time(new Date());
        int flag  = channelAppConfigMapper.add_channel_config(bean);
        if (flag == 1){
            /**新增数据时增加缓存*/
            String key = "CHANNEL_APP_CONFIG"+bean.getApp_id()+bean.getChannel_id();
            redisService.set(key,bean);
            return success(null);
        }
        return failed(ErrorCode.SYS_CAIYOUXISDK_ADD_ERROR.getCode(),ErrorCode.SYS_CAIYOUXISDK_ADD_ERROR.getMsg(), null);

    }


    /**
     修改游戏配置信息
     **/
    @Override
    @Transactional
    public ResData update_app_config(ChannelAppConfigBean bean) {
        bean.setUpdate_time(new Date());
        int flag  = channelAppConfigMapper.update_channel_config(bean);
        if (flag == 1){
            String key = "CHANNEL_APP_CONFIG"+bean.getApp_id()+bean.getChannel_id();
            /**修改数据清空缓存*/
            redisService.remove(key);
            /**再添加缓存*/
            redisService.set(key,bean);
            return success(null);
        }
        return failed(ErrorCode.SYS_CAIYOUXISDK_UPDATE_ERROR.getCode(),ErrorCode.SYS_CAIYOUXISDK_UPDATE_ERROR.getMsg(), null);

    }


    /**
     *删除游戏配置信息
     **/
    @Override
    @Transactional
    public ResData delete_app_config(ChannelAppConfigBean bean) {
        return null;
    }
}
